package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.mapper.UserMapper;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.Resident;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.User;
import com.wty.cmims.service.ResidentService;
import com.wty.cmims.mapper.ResidentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【resident】的数据库操作Service实现
* @createDate 2024-11-04 21:23:30
*/
@Service
public class ResidentServiceImpl extends ServiceImpl<ResidentMapper, Resident>
    implements ResidentService{
    @Autowired
    private  ResidentMapper residentMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult<Map<String, Object>> findResidentInfo(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        responseResult.setPageSize(pageSize);
        List<Map<String, Object>> residents = residentMapper.findResidentInfo(paramsMap);
        Integer total = residentMapper.findResidentInfoNum(paramsMap);
        if (residents != null) {
            responseResult.setDatas(residents);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("居民账号查询成功");
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("居民账号查询失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String,Object>> residentInfoUpdate(Map<String,Object> paramsMap) {
        ResponseResult<Map<String,Object>> responseResult = new ResponseResult<>();
        Integer rows = residentMapper.updateResidentInfo(paramsMap);
        if (rows == 2) {
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("居民信息更新成功");
            responseResult.setData(paramsMap);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("居民信息更新失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> residentResetPassword(Integer userId) {
//        ResponseResult<Void> responseResult = new ResponseResult<>();
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(User::getUserId,userId);
//        User user = this.getOne(wrapper);
//        Integer rows = userMapper.resetPassword(paramsMap);
//        if (rows == 1) {
//            responseResult.setCode(Code.SUCCESS);
//            responseResult.setMessage("密码重置成功");
//        } else {
//            responseResult.setCode(Code.ERROR);
//            responseResult.setMessage("密码重置失败");
//        }
        return null;
    }
}




